package edu.spec.student.service.impl;

import edu.spec.student.dao.iface.IEventDao;
import edu.spec.student.dao.iface.IStudentDao;
import edu.spec.student.dao.iface.IStudentEventDao;
import edu.spec.student.model.event.Event;
import edu.spec.student.model.event.StudentEvent;
import edu.spec.student.service.iface.IEventService;
import edu.spec.basic.model.enumeration.Status;
import org.apache.commons.io.FileUtils;
import org.madgecko.basic.model.Pager;
import org.madgecko.basic.model.SystemContext;
import org.springframework.stereotype.Service;

import javax.inject.Inject;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by MadFrog on 2017/5/20.
 */
@Service
public class EventService implements IEventService {

    @Inject
    private IEventDao eventDao;

    @Inject
    private IStudentEventDao studentEventDao;

    @Inject
    private IStudentDao studentDao;

    private String systemPath = SystemContext.getSystemPath();
    @Override
    public void add(Event e) {
        eventDao.add(e);
        try {
            // 1、将snapshoot从resources/browser/temp目录同步到resources/wechat/upload/images
//            String rootPath = SystemContext.getSystemPath();
            String srcPath = systemPath +  "\\resources\\temp\\" + e.getSnapshoot();
            String destPath = systemPath + "\\resources\\wechat\\upload\\images\\" + e.getSnapshoot();
            File srcFile = new File(srcPath);
            FileUtils.copyFile(srcFile, new File(destPath));
            // 2、删除零时文件
            srcFile.delete();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    @Override
    public void delete(Integer id) {
        // 首先删除相关的StudentEvent
        studentEventDao.deleteByEvent(id);
        // 其次删除关联的照片
        Event dbe = eventDao.load(id);
        String rootPath = SystemContext.getSystemPath();
        String srcPath = rootPath +  "\\resources\\wechat\\upload\\images\\" + dbe.getSnapshoot();
        File snapshoot = new File(srcPath);
        snapshoot.delete();
        // 最后删除Event
        eventDao.delete(id);

    }

    @Override
    public void update(Event e) {
        Event dbe = eventDao.load(e.getId());
        String systemPath = SystemContext.getSystemPath();
        File oldSnapshoot = new File(systemPath + "\\resources\\wechat\\upload\\images\\" + dbe.getSnapshoot());

        dbe.setName(e.getName());
        dbe.setEventDate(e.getEventDate());
        dbe.setLocation(e.getLocation());
        dbe.setWxNewsUrl(e.getWxNewsUrl());
        dbe.setStatus(e.getStatus());
        dbe.setSnapshoot(e.getSnapshoot());
        eventDao.update(dbe);
        // 保存snapshoot文件
        try {
            // 1、复制新文件
            File srcFile = new File(systemPath + "\\resources\\temp\\" + e.getSnapshoot());
            File destFile = new File(systemPath + "\\resources\\wechat\\upload\\images\\" + e.getSnapshoot());
            FileUtils.copyFile(srcFile, destFile);
            // 2、删除新文件的零时文件
            srcFile.delete();
            // 3、删除原来的旧文件
            oldSnapshoot.delete();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    @Override
    public Pager<Event> findByCon(Status status, String keyword) {
        return eventDao.findByCon(status, keyword);
    }

    @Override
    public Map<String, List<Event>> listStudentEvent(Integer id) {
        List<Event> latestEvent = eventDao.listLatestEvent();
        List<Event> attendedEvent = studentEventDao.listAttendedEvent(id);
        List<Event> unattendEvent = studentEventDao.listUnAttendEvent(id);
        Map<String, List<Event>> eventMap = new HashMap();
        eventMap.put("latestEvent", latestEvent);
        eventMap.put("attendedEvent", attendedEvent);
        eventMap.put("unattendEvent", unattendEvent);
        return eventMap;
    }

    @Override
    public Event load(Integer id) {
        return eventDao.load(id);
    }

    @Override
    public void attend(Integer studentId, Integer eventId) {
        StudentEvent se = new StudentEvent();
        se.setEvent(eventDao.load(eventId));
        se.setStudent(studentDao.load(studentId));
        studentEventDao.add(se);
    }

    @Override
    public void toggleStatus(Integer id) {
        Event e = eventDao.load(id);
        if(e.getStatus()==Status.DISABLED){
            e.setStatus(Status.ENABLED);
        }else if(e.getStatus()==Status.ENABLED){
            e.setStatus(Status.DISABLED);
        }
        eventDao.update(e);
    }
}
